﻿import request from "@/utils/request";
import { EnumDetectStatus } from "@/enums/business/detect.enum";
import { useSettingsStore } from "@/store";
import { detectRecordDetail } from "@/types/business/record";

const settingStore = useSettingsStore();
const BASE_URL = `/api/${settingStore.elementRoutePrefix}/detect`;

const DetectApi = {
  // 获取当前检测状态
  getDetectStatus() {
    return request<any, EnumDetectStatus>({
      url: `${BASE_URL}/GetCurrentStatus/`,
      method: "post",
    }).then((res) => {
      if (res == EnumDetectStatus.NotReady) return EnumDetectStatus.Ready;
      return res;
    });
  },

  /**
   * 参数确认，状态转为就绪
   */
  ready() {
    return request<any, void>({
      url: `${BASE_URL}/Ready/`,
      method: "post",
    });
  },

  /**
   * 开始检定
   * @param param
   */
  start(param: DetectStartParam) {
    return request<DetectStartParam, any>({
      url: `${BASE_URL}/Start/`,
      method: "post",
      data: param,
      headers: {
        "Content-Type": "application/json",
      },
    });
  },

  /**
   * 停止检定
   */
  stop() {
    return request<any, void>({
      url: `${BASE_URL}/Terminate/`,
      method: "post",
    });
  },

  /**
   * 暂停检定
   */
  pause() {
    return request<any, void>({
      url: `${BASE_URL}/Pause/`,
      method: "post",
    });
  },

  /**
   * 恢复检定
   */
  resume() {
    return request<any, void>({
      url: `${BASE_URL}/Resume/`,
      method: "post",
    });
  },

  /**
   * 获取检测记录缓存
   */
  getRecordDetailCache() {
    return request<any, detectRecordDetail[]>({
      url: `${BASE_URL}/GetRecordDetailCache/`,
      method: "post",
    });
  },
};

export interface DetectStartParam {
  useRobot: boolean;
  regulationId: number;
  toolingSensors: ToolModel[];
}
export interface ToolModel {
  stationNum: number;
  toolCode: string;
  slots: SlotModel[];
}
export interface SlotModel {
  slotNum: number;
  sensorId: number;
  sensorCode: string;
  sensorName: string;
}

export default DetectApi;
